/**
 * Created by fld on 2021/09/11.
 */
var domain = window.location.protocol+"//"+window.location.host;
function uploadImg(id){
	var url = domain + "/image/index?id="+id;
	layer.open({
		type: 2,
		title: '图库',
		shadeClose: true,
		shade: false,
	  	maxmin: true, //开启最大化最小化按钮
	  	area: ['1200px', '600px'],
	  	content: url
	});
}
/*$('.group_div .item').hover(function(){
	$(this).find('.btn-group').show();
},function(){
	$(this).find('.btn-group').hide();
});*/
$('.group_div .item .group_a').click(function(){
	var id = $(this).attr('data-id');
	$('#group_id').val(id);
	$('.search-bar').submit();
})
$('.addGroup').click(function(){
	var url = domain + "/image/add_group";
	layer.open({
		type: 2,
		title: '添加图片分组',
		shadeClose: true,
		shade: false,
	  	maxmin: true, //开启最大化最小化按钮
	  	area: ['500px', '350px'],
	  	content: url
	});
})

$('.submitForm').click(function(){
    var form = $(this).parents('form');
    loadIndex = layer.load(2);
    $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize(),
        dataType: 'json',
        success: function(result){
            layer.close(loadIndex);
            if(result.status){
                layer.msg(result.message, {
                    time: 1000,
                    icon: 6,
                    end: function () {
                    	closeWindow();
                    	parent.location.reload();
                    }
                });
            }else{
                layer.msg(result.message, {
                    icon: 5,
                    time: 2000
                });
            }
        }
    });
})

$('.deleteGroup').click(function(){
	var url = $(this).attr('data-href');
    var confirm_msg = $(this).attr('data-confirm');
    var id = $(this).attr('data-id');

    layer.confirm(confirm_msg, {
        btn: ['确定删除','取消'] 
    }, function(){
        loadIndex = layer.load(2);
        $.ajax({
            type: 'post',
            url: url,
            data: {id:id},
            dataType: 'json',
            success: function(result){
                layer.close(loadIndex);
                if(result.status){
                	$('#group_id').val('');
                    layer.msg(result.message, {
                        time: 1000,
                        icon: 6,
                        end: function () {
                        	$('.search-bar').submit();
                        }
                    });
                }else{
                    layer.msg(result.message, {
                        icon: 5,
                        time: 2000
                    });
                }
            }
        });
    }, function(){
        layer.closeAll();
    });
})

$('.editGroup').click(function(){
	var url = $(this).attr('data-href');
    var id = $(this).attr('data-id');
	layer.open({
		type: 2,
		title: '修改图片分组',
		shadeClose: true,
		shade: false,
	  	maxmin: true, //开启最大化最小化按钮
	  	area: ['500px', '350px'],
	  	content: url+"?id="+id
	});
})

$('.searchGroup').click(function(){
	var group_name = $.trim($('#group_name').val());
	if(group_name != '' && group_name != null){
		var url = domain + "/image/index" + "?group_name="+group_name;
	}else{
		var url = domain + "/image/index";
	}
	location.href = url;
})

$('.file_upload .file').change(function(){
	var formData = new FormData();
	formData.append("img_file", this.files[0]);
	var group_id = $('.group_div .active').find('.group_a').attr('data-id');
	formData.append("group_id", group_id);
	var url = domain + "/image/upload";
	loadIndex = layer.load(2);
    $.ajax({
        type: 'post',
        url: url,
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function(result){
            layer.close(loadIndex);
            if(result.status){
                layer.msg(result.message, {
                    time: 1000,
                    icon: 6,
                    end: function () {
                    	location.reload();
                    }
                });
            }else{
                layer.msg(result.message, {
                    icon: 5,
                    time: 2000
                });
            }
        }
    });
})

$('.img_list .delete_img').click(function(){
	var id = $(this).attr('data-id');
	var url = domain + "/image/delete_img";
	var confirm_msg = $(this).attr('data-confirm');
    layer.confirm(confirm_msg, {
        btn: ['确定删除','取消'] 
    }, function(){
        loadIndex = layer.load(2);
        $.ajax({
            type: 'post',
            url: url,
            data: {'id': id},
            dataType: 'json',
            success: function(result){
                layer.close(loadIndex);
                if(result.status){
                    layer.msg(result.message, {
                        time: 1000,
                        icon: 6,
                        end: function () {
                        	location.reload();
                        }
                    });
                }else{
                    layer.msg(result.message, {
                        icon: 5,
                        time: 2000
                    });
                }
            }
        });
    }, function(){
        layer.closeAll();
    });
})

$('.page-link').click(function(){
    var url = $(this).attr('data-href');
    if(url != ''){
        $('.search-bar').attr('action', url);
        $('.search-bar').submit();
    }
})

$('#sortby').change(function(){
	$('.search-bar').submit();
})

$('.select_img').click(function(){
	var img_url = $(this).attr('data-img-url');
	var url = $(this).attr('data-url');
	var id = $('#id').val();
	parent.$('.pic-add:before').attr('content','');
	parent.$('#'+id).val(img_url);
	parent.$('.'+id+'_img').attr('src', url);
	parent.layer.closeAll();
})

$('.pic-add').hover(function(){
	$(this).find('.deleteImg').show();
},function(){
	$(this).find('.deleteImg').hide();
});

$('.deleteImg').click(function(){
	var data_id = $(this).attr('data-id');
	$('.'+data_id+'_img').attr('src', '');
	$('#'+data_id).val('');
});

function updateGroup(){
	var ids = new Array();
	$('.item .group_a').each(function(){
		var id = $(this).attr('data-id');
		if($.inArray(id, ids) == -1){
			ids.push(id);
		}
	})
	url = domain + "/image/update_group";
	loadIndex = layer.load(2);
    $.ajax({
        type: 'post',
        url: url,
        data: {'id': ids.toString()},
        dataType: 'json',
        success: function(result){
        	layer.close(loadIndex);
        }
    });
}

function closeWindow(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}